import {
    createRouter,
    createWebHistory,
    createWebHashHistory
} from 'vue-router'

import PagesVue from '../Pages/pages.vue'
import Login from '../Pages/login.vue'

function getPageRouter() {
    const pageRouter = [{
        name: "resourcesTree",
        path: "resourcesTree",
        meta: {
            title: "资源树",
            icon: "resourceTree",
        },
        children: [{
            name: "baseResourcesTree",
            path: "baseResourcesTree",
            component: () => import('../Pages/page/resourcesTree.vue'),
            meta: {
                title: "基础资源树",
                icon: "collection"
            }
        }, {
            name: "resourcesTreeBIM",
            path: "resourcesTreeBIM",
            component: () => import('../Pages/page/resourcesTreeBIM.vue'),
            meta: {
                title: "bim模型分析",
                icon: "bim"
            }
        }, {
            name: "resourcesTreeWhite",
            path: "resourcesTreeWhite",
            component: () => import('../Pages/page/resourcesTreeWhite.vue'),
            meta: {
                title: "白模分析",
                icon: "white_model"
            }
        }, {
            name: "animation",
            path: "animation",
            component: () => import('../Pages/page/animation.vue'),
            meta: {
                title: "动画",
                icon: "animation"
            }
        }]
    }, {
        name: "masterTool",
        path: "masterTool",
        meta: {
            title: "基础工具",
            icon: "masterTool"
        },
        children: [{
                name: "keyboardRoaming",
                path: "keyboardRoaming",
                component: () => import('../Pages/page/keyboardRoaming.vue'),
                meta: {
                    title: "键盘漫游",
                    icon: "keyboardRoaming"
                }
            }, {
                name: "mutilScreenComparision",
                path: "mutilScreenComparision",
                component: () => import('../Pages/page/mutilScreenComparision.vue'),
                meta: {
                    title: "多屏对比",
                    icon: "mutilScreen"
                }
            }, {
                name: "split",
                path: "split",
                component: () => import('../Pages/page/split.vue'),
                meta: {
                    title: "卷帘对比",
                    icon: "split"
                }
            }, {
                name: "weather",
                path: "weather",
                component: () => import('../Pages/page/weather.vue'),
                meta: {
                    title: "天气",
                    icon: "weather"
                }
            }, {
                name: "roamingPath",
                path: "roamingPath",
                component: () => import('../Pages/page/roamingPath.vue'),
                meta: {
                    title: "漫游路径",
                    icon: "path"
                }
            }, {
                name: "singleSelect",
                path: "singleSelect",
                component: () => import('../Pages/page/selectTool.vue'),
                meta: {
                    title: "单选",
                    icon: "singleSelect"
                }
            }, {
                name: "boxSelect",
                path: "boxSelect",
                component: () => import('../Pages/page/selectTool.vue'),
                meta: {
                    title: "框选",
                    icon: "boxSelect"
                }
            }, {
                name: "setSkyline",
                path: "setSkyline",
                component: () => import('../Pages/page/setSkyline.vue'),
                meta: {
                    title: "设置天际线",
                    icon: "setSkyline"
                }
            }, {
                name: "skyBox",
                path: "skyBox",
                component: () => import('../Pages/page/skyBox.vue'),
                meta: {
                    title: "天空盒",
                    icon: "space"
                }
            },
            {
                name: "curtain",
                path: "curtain",
                component: () => import('../Pages/page/curtain.vue'),
                meta: {
                    title: "图像融合（幕布）",
                }
            }, {
                name: "facePatch",
                path: "facePatch",
                component: () => import('../Pages/page/facePatch.vue'),
                meta: {
                    title: "视频融合（地表）",
                }
            }
        ]
    }, {
        name: "surface",
        path: "surface",
        meta: {
            title: "地表功能",
            icon: "surface"
        },
        children: [{
                name: "surveillance",
                path: "surveillance",
                component: () => import('../Pages/page/surveillance.vue'),
                meta: {
                    title: "视频监控",
                    icon: "video"
                }
            },
            {
                name: "mark",
                path: "mark",
                component: () => import('../Pages/page/mark.vue'),
                meta: {
                    title: "标注",
                    icon: "mark"
                }
            },
            {
                name: "militaryMark",
                path: "militaryMark",
                component: () => import('../Pages/page/militaryMark.vue'),
                meta: {
                    title: "军事标注",
                    icon: "mark"
                }
            },
            {
                name: "modelPlant",
                path: "modelPlant",
                component: () => import('../Pages/page/symbolManager/index.vue'),
                meta: {
                    title: "模型种植",
                    icon: "modelPlant"
                }
            }
        ]
    }, {
        name: "system",
        path: "system",
        meta: {
            title: "系统管理",
            icon: "system"
        },
        children: [{
            name: "userManage",
            path: "userManage",
            component: () => import('../Pages/page/userManage.vue'),
            meta: {
                title: "用户管理",
                icon: "user",
            }
        }, {
            name: "roleManage",
            path: "roleManage",
            component: () => import('../Pages/page/roleManage.vue'),
            meta: {
                title: "角色管理",
                icon: "role"
            }
        }, {
            name: "sceneManage",
            path: "sceneManage",
            component: () => import('../Pages/page/sceneManage.vue'),
            meta: {
                title: "场景管理",
                icon: "scene"
            }
        }, {
            name: "layerManager",
            path: "layerManager",
            component: () => import('../Pages/page/layerManager.vue'),
            meta: {
                title: "图层管理",
                icon: "layer",
            }
        },{
            name: "videoManager",
            path: "videoManager",
            component: () => import('../Pages/page/surveillanceManager/index.vue'),
            meta: {
                title: "视频管理",
                icon: "video"
            }
        },{
            name: "animationManage",
            path: "animationManage",
            component: () => import('../Pages/page/animationManage/index.vue'),
            meta: {
                title: "动画管理",
                icon: "animation"
            }
        }]
    }, {
        name: "space",
        path: "space",
        meta: {
            title: "空间分析",
            icon: "space"
        },
        children: [{
                name: "bufferAnalysis",
                path: "bufferAnalysis",
                component: () => import('../Pages/page/bufferAnalysis.vue'),
                meta: {
                    title: "缓冲分析",
                    icon: "buffer"
                }
            }, {
                name: "sectionAnalysis",
                path: "sectionAnalysis",
                component: () => import('../Pages/page/sectionAnalysis.vue'),
                meta: {
                    title: "剖面分析",
                    icon: "section"
                }
            }, {
                name: "slopeAnalysis",
                path: "slopeAnalysis",
                component: () => import('../Pages/page/slopeAnalysis.vue'),
                meta: {
                    title: "坡度分析",
                    icon: "slope"
                }
            }, {
                name: "measure",
                path: "measure",
                component: () => import('../Pages/page/measure.vue'),
                meta: {
                    title: "测量工具",
                    icon: "measure"
                }
            },
            {
                name: "viewShed",
                path: "viewShed",
                component: () => import('../Pages/page/viewShed.vue'),
                meta: {
                    title: "可视域分析",
                    icon: "viewShed"
                }
            }, {
                name: "sightline",
                path: "sightline",
                component: () => import('../Pages/page/sightline.vue'),
                meta: {
                    title: "通视分析",
                    icon: "sightline"
                }
            }, {
                name: "limitHeight",
                path: "limitHeight",
                component: () => import('../Pages/page/limitHeight.vue'),
                meta: {
                    title: "限高分析",
                    icon: "limitHeight"
                }
            }, {
                name: "shadow",
                path: "shadow",
                component: () => import('../Pages/page/shadow.vue'),
                meta: {
                    title: "日照分析",
                    icon: "shadow"
                }
            }
        ]
    }]

    pageRouter.forEach(item => {
        if (item.children) {
            item.children.forEach(({
                meta
            }) => {
                meta.parentTitle = item.meta.title
            })
        }
    })
    return pageRouter
}

const pageRouter = getPageRouter()

const routes = [{
        path: "/",
        component: PagesVue
    },
    {
        path: "/login",
        component: Login
    },
    {
        name: "page",
        path: "/page",
        component: PagesVue,
        children: pageRouter
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, form, next) => {
    if (to.path !== '/login') {
        if (window.sessionStorage.getItem("userId")) {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }
})

export default router

export const menuMap = getMenuMap()

function getMenuMap() {
    const map = {}
    recursion(pageRouter)

    function recursion(pageRouter) {
        pageRouter.forEach(item => {
            map[item.meta.title] = {
                icon: item.meta.icon,
                name: item.meta.title,
                routerName: item.name
            }
            if (item.children && item.children.length) {
                recursion(item.children)
            }
        })
    }
    return map
}